from xgolib import XGO
from xgoedu import XGOEDU
import time

class DogController:
    def __init__(self,xgo_edu):
        self.dog = XGO("xgolite")
        self.direct_map = {
            '左':'y',
            '右':'Y',
            '前':'x',
            '后':'X'
        }
        self.xgo_edu = xgo_edu
    # 从圆柱体中抓取
    def catch_from_cylinder(self):
        self.dog.claw(0)
        time.sleep(1)
        self.dog.motor(11,-30)
        self.dog.motor(21,-30)
        time.sleep(0.5)
        # 张开
        self.dog.arm(100,-30)
        time.sleep(1)
        self.dog.claw(255)
        time.sleep(1)
        self.dog.reset()
        time.sleep(2)
    def Crouch(self):
        '''下蹲'''
        self.dog.motor(11, -30)
        self.dog.motor(21, -30)
    def Reset(self):
        self.dog.reset()
    # 放到圆柱体上
    def put_on_cylinder(self):
        self.dog.motor(11,-30)
        self.dog.motor(21,-30)
        time.sleep(0.5)
        self.dog.arm(100,-30)
        time.sleep(1)
        self.dog.claw(0)
        time.sleep(1)
        self.dog.reset()
        time.sleep(2)
    # 从地上夹取长方条
    def catch_from_ground(self):
        self.dog.claw(0)
        time.sleep(1)
        self.dog.motor(11,-30)
        self.dog.motor(21,-30)
        time.sleep(0.5)
        # 张开
        self.dog.arm(100,-50)
        time.sleep(1)
        self.dog.claw(255)
        time.sleep(1)
        self.dog.reset()  
        time.sleep(2)
    def move(self,direct,t):
        direct = self.direct_map[direct]
        if direct == 'X' or direct == 'Y':
            self.dog.move(direct.lower(),-20)
        else:
            self.dog.move(direct,20)
        time.sleep(t)
        self.dog.stop() 
        time.sleep(0.1)
        self.dog.reset()
    def turn(self,angle,t):
        self.dog.turn(angle)
        time.sleep(t)
        self.dog.stop()
    def Speech(self,text):
        self.xgo_edu.SpeechSynthesis(text)